package com.cms.controller;

import com.cms.entity.Freelance;
import com.cms.exception.DuplicateKeyException;
import com.cms.model.Result;
import com.cms.service.FreelanceService;
import com.cms.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(path = "/freelance")
public class FreelanceController {
    @Autowired
    private FreelanceService freelanceService;

    @ResponseBody
    @RequestMapping (path = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        List<Freelance> freelanceList = freelanceService.findAll();
        return ResultUtil.success(freelanceList.size(), freelanceList);
    }

    @ResponseBody
    @RequestMapping (path = "/insertFreelance", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public Result insertFreelance(@RequestBody HashMap<String,String> insertMap){

        Freelance freelance = new Freelance(
                insertMap.get("freelance_id"),
                insertMap.get("freelance_name"),
                insertMap.get("freelance_tel"),
                insertMap.get("freelance_email"),
                insertMap.get("freelance_address"),
                insertMap.get("freelance_type")
        );

        try {
            if (freelanceService.insertFreelance(freelance) == 1) {
                return ResultUtil.success();
            } else {
                return ResultUtil.error();
            }
        } catch (DuplicateKeyException e) {
            return ResultUtil.error();
        }
    }

    @ResponseBody
    @RequestMapping (path = "/updateFreelance", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public Result updateFreelance(@RequestBody HashMap<String,String> updateMap){
        try {
            System.out.println(updateMap);

            if (freelanceService.updateFreelance(updateMap) == 1) {
                System.out.println(freelanceService.updateFreelance(updateMap));
                return ResultUtil.success();
            } else {
                System.out.println(freelanceService.updateFreelance(updateMap));
                return ResultUtil.error();
            }
        } catch (DuplicateKeyException e) {
            return ResultUtil.error();
        }
    }

    @ResponseBody
    @RequestMapping (path = "/deleteFreelance", method = RequestMethod.GET)
    public Result deleteFreelance(String id) {
        System.out.println(id);
        if (freelanceService.deleteFreelance(id) == 1) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error();
        }
    }

    @ResponseBody
    @RequestMapping (path = "/findFreelanceBy", method = RequestMethod.GET)
    public Result findFreelanceBy(int selects, String findContent){

        List<Freelance> freelanceList;
        switch (selects){
            case 0 :
                freelanceList = freelanceService.findFreelanceById(findContent);
                break;
            case 1 :
                freelanceList = freelanceService.findFreelanceByName(findContent);
                break;
            case 2 :
                freelanceList = freelanceService.findFreelanceByTelephone(findContent);
                break;
            case 3 :
                freelanceList = freelanceService.findFreelanceByEmail(findContent);
                break;
            case 4 :
                freelanceList = freelanceService.findFreelanceByType(findContent);
                break;
            default:
                freelanceList = freelanceService.findFreelanceById(findContent);
        }

        if ( freelanceList != null) {
            return ResultUtil.success(freelanceList.size(), freelanceList);
        } else {
            return ResultUtil.error();
        }
    }
}
